/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.servlet.member;

import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.dao.ProjectDAO;
import model.dao.ProjectTypeDAO;
import model.dao.ProjectUserDAO;
import model.dao.UserDAO;
import model.dao.service.ProjectDAOService;
import model.dao.service.ProjectTypeDAOService;
import model.dao.service.ProjectUserDAOService;
import model.dao.service.UserDAOService;
import model.entities.Project;
import model.entities.ProjectType;
import model.entities.ProjectUserDetails;
import model.entities.User;

/**
 *
 * @author Admin
 */
public class PostProject extends HttpServlet {

    ProjectDAOService PROJECT_SERVICE = ProjectDAO.getInstance();
    ProjectTypeDAOService TYPE_SERVICE = ProjectTypeDAO.getInstance();
    ProjectUserDAOService PU_SERVICE = ProjectUserDAO.getInstance();
    UserDAOService USER_SERVICE = UserDAO.getInstance();

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        int projectID = Integer.parseInt(request.getParameter("id"));
        Project project = PROJECT_SERVICE.getProjectByID(projectID);
        request.setAttribute("currentProject", project);
        request.getRequestDispatcher("member/content/add_user_project.jsp").forward(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        if (request.getCharacterEncoding() == null) {
            request.setCharacterEncoding("UTF-8");
        }
        String action = request.getParameter("submit");
        switch (action) {
            case "Create Project":
                createProject(request, response);
                break;
            case "Apply":
                AddUserJoiningProject(request, response);
                break;
        }
    }

    private void createProject(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        if (request.getCharacterEncoding() == null) {
            request.setCharacterEncoding("UTF-8");
        }
        String projectName = request.getParameter("projectName");
        String day = request.getParameter("day");
        String month = request.getParameter("month");
        String year = request.getParameter("year");
        Date startDate = util.Support.convertToDate(day, month, year);
        String dtype = request.getParameter("dtype");
        int value = Integer.parseInt(request.getParameter("duration"));
        int duration;
        switch (dtype) {
            case "day":
                duration = value;
                break;
            case "week":
                duration = value * 7;
                break;
            default:
                duration = value * 30;
                break;
        }
        String projectType = request.getParameter("projectType");
        String description = request.getParameter("description");
        ProjectType type = TYPE_SERVICE.getTypeByName(projectType);

        Project project = new Project(1, projectName, description, startDate, duration, type, true);
        if (PROJECT_SERVICE.createProject(project)) {
            User user = (User) request.getSession().getAttribute(util.Constants.CURRENT_USER);
            if (user == null) {
                response.getWriter().print("Đăng nhập lại");
            } else {
                List<Project> projectList = PROJECT_SERVICE.getProjectByName(projectName);
                Project p = projectList.get(projectList.size() - 1);
                ProjectUserDetails pud = new ProjectUserDetails(1, user, p, true);
                PU_SERVICE.createPUD(pud);
                response.getWriter().print("OK");
            }
        } else {
            response.getWriter().print("FAILED");
        }
    }

    private void AddUserJoiningProject(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        if (request.getCharacterEncoding() == null) {
            request.setCharacterEncoding("UTF-8");
        }
        int projectID = Integer.parseInt(request.getParameter("id"));
        String[] users = request.getParameterValues("userJoined");
        if (users != null) {
            PU_SERVICE.deleteUserJoinedProject(projectID);
            for (String userList : users) {
                User user = USER_SERVICE.getUserByUserName(userList);
                Project project = PROJECT_SERVICE.getProjectByID(projectID);
                ProjectUserDetails pud = new ProjectUserDetails(1, user, project, false);
                if (!PU_SERVICE.checkJoinUser(user, project)) {
                    if (PU_SERVICE.createPUD(pud)) {
                        response.getWriter().print("OK");
                    } else {
                        response.getWriter().print("Loi");
                    }
                }
            }
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }

}
